//
//  PicViewController.m
//  Today
//
//  Created by dllo on 15/5/30.
//  Copyright (c) 2015年 fanfan. All rights reserved.
//

#import "PicViewController.h"

@interface PicViewController ()

@end

@implementation PicViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view.
    activity = [[UIActivityIndicatorView alloc]initWithActivityIndicatorStyle:UIActivityIndicatorViewStyleWhiteLarge];
    activity.tintColor = [UIColor colorWithRed:0.368 green:0.377 blue:0.500 alpha:1.000]
    ;
    activity.frame = CGRectMake(0, 0, 200, 200);
    activity.center = self.view.center;
    [self.view addSubview:activity];
    [activity startAnimating];
    [self loadpic];
    
}
- (void)initview
{
    
    UIScrollView *scroll = [[UIScrollView alloc] initWithFrame:CGRectMake(0, 0, self.view.frame.size.width, self.view.frame.size.height )];
    scroll.showsHorizontalScrollIndicator = NO;
    scroll.showsVerticalScrollIndicator = NO;
    scroll.bounces = NO;
    scroll.backgroundColor = [UIColor colorWithRed:0.043 green:0.500 blue:0.017 alpha:1.000]
    ;
    scroll.pagingEnabled = YES;
    scroll.contentSize = CGSizeMake(self.view.frame.size.width * self.picArray.count, self.view.frame.size.height );
    scroll.contentOffset = CGPointMake(0, 0);
    
    for (int i = 0; i < self.picArray.count; i++) {
        UIImageView *imageview = [[UIImageView alloc] initWithFrame:CGRectMake(scroll.frame.size.width * i, 0, scroll.frame.size.width, scroll.frame.size.height- 200)];
        imageview.image = self.picArray[i];
        imageview.contentMode = UIViewContentModeScaleAspectFit;
        [scroll addSubview:imageview];
        UILabel *label = [[UILabel alloc] initWithFrame:CGRectMake(scroll.frame.size.width * i, self.view.frame.size.height - 200, scroll.frame.size.width, 150)];
        label.text = self.titleArray[i];
        label.numberOfLines = 6;
        label.textAlignment = NSTextAlignmentCenter;
        [scroll addSubview:label];
    }
    
    [self.view addSubview:scroll];
    UIImage *image = [UIImage imageNamed:@"iconfont-paizhao"];
    self.navigationItem.rightBarButtonItem = [[UIBarButtonItem alloc]initWithImage:image style:UIBarButtonItemStyleDone target:self action:@selector(sellectorMy)];
    self.navigationItem.leftBarButtonItem = [[UIBarButtonItem alloc]initWithImage:[UIImage imageNamed:@"iconfont-fanhui-2"] style:UIBarButtonItemStyleDone target:self action:@selector(black)];
}
- (void)black
{
    [self.navigationController popViewControllerAnimated:YES];
}
- (void)sellectorMy
{
    PhotoViewController *photo1 = [[PhotoViewController alloc]init];
    [self.navigationController pushViewController:photo1 animated:YES];
    UIActionSheet *action = [[UIActionSheet alloc]initWithTitle:@"请分享方式" delegate:self cancelButtonTitle:@"取消" destructiveButtonTitle:@"拍照" otherButtonTitles:@"本地相册", nil];
    [action showInView:self.view];
    
    
}
- (void)loadpic
{
    NSString *string = [NSString stringWithFormat:@"http://qianming.sinaapp.com/index.php/AndroidApi23/index/cid/qutu/p/1/markId/0"];
    string = [string stringByAddingPercentEscapesUsingEncoding:NSUTF8StringEncoding];
    NSURL *url = [NSURL URLWithString:string];
    NSMutableURLRequest *request = [NSMutableURLRequest requestWithURL:url cachePolicy:NSURLRequestUseProtocolCachePolicy timeoutInterval:60];
    request.HTTPMethod = @"GET";
    self.picArray = [NSMutableArray array];
    self.titleArray = [NSMutableArray array];
    [NSURLConnection sendAsynchronousRequest:request queue:[NSOperationQueue mainQueue] completionHandler:^(NSURLResponse *response, NSData *data, NSError *connectionError) {
        [activity stopAnimating];
        NSDictionary *dic = [NSJSONSerialization JSONObjectWithData:data options:NSJSONReadingMutableContainers error:nil];
        NSArray *array = [dic objectForKey:@"rows"];
        for (NSDictionary *dic in array) {
            NSString *str = [dic objectForKey:@"pic"];
            NSString *title = [dic objectForKey:@"title"];
            str = [str stringByAddingPercentEscapesUsingEncoding:NSUTF8StringEncoding];
            NSURL *picurl = [NSURL URLWithString:str];
            NSData *data = [NSData dataWithContentsOfURL:picurl];
            UIImage *image = [UIImage imageWithData:data];
            [self.titleArray addObject:title];
            [self.picArray addObject:image];
        }
        [self initview];
    }];
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
